import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

/* Layout */
import Layout from '../layout/Layout'

/**
 * Note: sub-menu only appear when route children.length >= 1
 * Detail see: https://panjiachen.github.io/vue-element-admin-site/guide/essentials/router-and-nav.html
 *
 * hidden: true                   if set true, item will not show in the sidebar(default is false)
 * alwaysShow: true               if set true, will always show the root menu
 *                                if not set alwaysShow, when item has more than one children route,
 *                                it will becomes nested mode, otherwise not show the root menu
 * redirect: noRedirect           if set noRedirect will no redirect in the breadcrumb
 * name:'router-name'             the name is used by <keep-alive> (must set!!!)
 * meta : {
    roles: ['admin','editor']    control the page roles (you can set multiple roles)
    title: 'title'               the name show in sidebar and breadcrumb (recommend set)
    icon: 'svg-name'             the icon show in the sidebar
    breadcrumb: false            if set false, the item will hidden in breadcrumb(default is true)
    activeMenu: '/example/list'  if set path, the sidebar will highlight the path you set
  }
 */

/**
 * constantRoutes
 * a base page that does not have permission requirements
 * all roles can be accessed
 */
export const constantRoutes = [
    { path: '/login', component: () => import('@/views/login/index'), hidden: true },
    { path: '/404', component: () => import('@/views/features/404'), hidden: true },
    { path: '/401', component: () => import('@/views/features/401'), hidden: true },
    { path: '/redirect', component: Layout, hidden: true,  //配合路由结尾去掉 *404，一起实现右键tagview实现刷新
        children: [
            {
                path: '/redirect/:path*',
                component: () => import('@/views/features/redirect')
            }
        ]
    },
{
    path: '/',
    component: Layout,
    redirect: '/index',
    children: [{
        path: 'index',
        name: 'index',
        component: () => import('@/views/dashboard/index'),
        meta: {
            title: '首页',
            icon: 'dashboard',
            affix: true
        }
    }]
},

{
    path: '/system',
    component: Layout,
    redirect: '/system/user',
    meta: { title: '系统管理', icon: 'form' },
    children: [
        {
            path: 'user',
            name: 'user',
            component: () => import('@/views/system/user/index'),
            meta: {
                title: '用户管理',
                icon: 'dashboard'
            }
        },
        {
            path: 'code',
            name: 'code',
            component: () => import('@/views/dashboard/index'),
            meta: {
                title: '字典管理',
                icon: 'dashboard'
            }
        },
        {
            path: 'role',
            name: 'role',
            component: () => import('@/views/system/role/index'),
            meta: {
                title: '角色管理',
                icon: 'dashboard'
            }
        },
        {
            path: 'log',
            name: 'log',
            component: () => import('@/views/system/log/index'),
            meta: {
                title: '日志管理',
                icon: 'dashboard'
            }
        }
    ]
},
{
    path: '/blog',
    component: Layout,
    redirect: '/blog/category',
    meta: { title: '博客管理', icon: 'form' },
    children: [
        {
            path: 'category',
            name: 'category',
            component: () => import('@/views/blog/category/index'),
            meta: {
                title: '文章分类管理',
                icon: 'dashboard'
            }
        },
        {
            path: 'article',
            name: 'article',
            component: () => import('@/views/blog/article/index'),
            meta: {
                title: '文章管理',
                icon: 'dashboard'
            },
        },
        {
            path: 'whisper',
            name: 'whisper',
            component: () => import('@/views/blog/whisper/index.vue'),
            meta: {
                title: '微言管理',
                icon: 'dashboard'
            }
        },
        {
            path: 'comment',
            name: 'comment',
            component: () => import('@/views/blog/comment/index.vue'),
            meta: {
                title: '留言管理',
                icon: 'dashboard'
            }
        },
        {
            path: 'album',
            name: 'album',
            component: () => import('@/views/blog/album/index.vue'),
            meta: {
                title: '相册管理',
                icon: 'dashboard'
            }
        }
    ]
},
{
    path: '/article',component:Layout,redirect:'/article/write', hidden:true,
    children: [
        {path: 'write',name: 'articleWrite',component: () => import('@/views/blog/article/pages/WriteAndEdit'),meta: {title: '写文章',}},
        {path: 'edit', name: 'articleEdit', component: () => import('@/views/blog/article/pages/WriteAndEdit'),meta: {title: '编辑文章', }},
    ]
},
{
    path: '/whisper',component:Layout,redirect:'/whisper/write', hidden:true,
    children: [
        {path: 'write',name: 'whisperWrite',component: () => import('@/views/blog/whisper/pages/WriteAndEdit'),meta: {title: '写微言',}},
        {path: 'edit', name: 'whisperEdit', component: () => import('@/views/blog/whisper/pages/WriteAndEdit'),meta: {title: '编辑微言', }},
    ]
},
{
    path: '/album',component:Layout,redirect:'/album/uploadImg', hidden:true,
    children: [
        {path: 'uploadImg',name: 'albumUploadImg',component: () => import('@/views/blog/album/pages/UploadImg'),meta: {title: '上传照片',}},
        {path: 'uploadImg',name: 'albumUploadImg',component: () => import('@/views/blog/album/pages/UploadImg'),meta: {title: '上传照片',}},
    ]
},
{
    path: '/tools',
    component: Layout,
    // redirect: '/tools/category',
    meta: { title: '系统工具', icon: 'form' },
    children: [
        {
            path: 'swagger',
            name: 'swagger',
            component: () => import('@/views/systools/swagger/index'),
            meta: {
                title: '接口文档',
                icon: 'dashboard'
            }
        },
        {
            path: 'job',
            name: 'job',
            component: () => import('@/views/systools/job/index'),
            meta: {
                title: '定时任务',
                icon: 'dashboard'
            }
        },
    ]
},
{
    path: '/user',
    component: Layout,
    hidden: true,
    redirect: 'noredirect',
    children: [
      {
        path: 'center',
        component: () => import('@/views/system/center/index.vue'),
        name: 'center',
        meta: { title: '个人中心', icon: 'user' }
      }
    ]
  },
{
    path: '/example',
    component: Layout,
    redirect: '/example/table',
    name: 'Example',
    meta: {
        title: 'Example',
        icon: 'example'
    },
    children: [{
        path: 'table',
        name: 'Table',
        component: () => import('@/views/table/index'),
        meta: {
            title: '表格',
            icon: 'table'
        }
    },
    {
        path: 'tree',
        name: 'Tree',
        component: () => import('@/views/tree/index'),
        meta: {
            title: '树形控件',
            icon: 'tree'
        }
    }
    ]
},

{
    path: '/form',
    component: Layout,
    children: [{
        path: 'index',
        name: '表单',
        component: () => import('@/views/form/index'),
        meta: {
            title: '表单控件',
            icon: 'form'
        }
    }]
},

{
    path: '/nested',
    component: Layout,
    redirect: '/nested/menu1',
    name: 'nested',
    meta: {
        title: '多级菜单',
        icon: 'nested'
    },
    children: [{
        path: 'menu1',
        component: () => import('@/views/nested/menu1/index'), // Parent router-view
        name: 'Menu1',
        meta: {
            title: '一级菜单-1'
        },
        children: [{
            path: 'menu1-1',
            component: () => import('@/views/nested/menu1/menu1-1'),
            name: 'Menu1-1',
            meta: {
                title: '二级菜单-1'
            }
        },
        {
            path: 'menu1-2',
            component: () => import('@/views/nested/menu1/menu1-2'),
            name: 'Menu1-2',
            meta: {
                title: '二级菜单-2'
            },
            children: [{
                path: 'menu1-2-1',
                component: () => import('@/views/nested/menu1/menu1-2/menu1-2-1'),
                name: 'Menu1-2-1',
                meta: {
                    title: '三级菜单-1'
                }
            },
            {
                path: 'menu1-2-2',
                component: () => import('@/views/nested/menu1/menu1-2/menu1-2-2'),
                name: 'Menu1-2-2',
                meta: {
                    title: '三级菜单-2'
                }
            }
            ]
        },
        {
            path: 'menu1-3',
            component: () => import('@/views/nested/menu1/menu1-3'),
            name: 'Menu1-3',
            meta: {
                title: '二级菜单-3'
            }
        }
        ]
    },
    {
        path: 'menu2',
        component: () => import('@/views/nested/menu2/index'),
        meta: {
            title: '一级菜单-2'
        }
    }
    ]
},
{
    path: '/test',
    component: Layout,
    redirect: '/test/test1',
    name: '测试页面',
    meta: {
        title: 'Example',
        icon: 'example'
    },
    children: [
        {
            path: 'test1',
            name: 'test1',
            component: () => import('@/views/test/test1'),
            meta: {
                title: '页面转图片',
                icon: 'table'
            }
        },
    ]
},
{
    path: 'external-link',
    component: Layout,
    children: [{
        path: 'https://panjiachen.github.io/vue-element-admin-site/#/',
        meta: {
            title: '外链',
            icon: 'link'
        }
    }]
},

// 404 page must be placed at the end !!!
{ path: '*', redirect: '/404', hidden: true} //如果不隐藏，刷新tagview会去404页面
]

const createRouter = () => new Router({
    // mode: 'history', // require service support
    scrollBehavior: () => ({
        y: 0
    }),
    routes: constantRoutes
})

const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
    const newRouter = createRouter()
    router.matcher = newRouter.matcher // reset router
}

export default router
